<template>
	<section class="vbox">
		<section class="scrollable padder">
			<section class="panel panel-default m-t-sm">
				<header class="panel-heading">
					角色管理
					<ul class="nav nav-tabs pull-right no-border">
						<li class="m-r-sm m-t-xs">
							<el-input placeholder="输入角色名称" v-model="keywords" clearable>
								<template #append>
									<el-button slot="append" type="success" icon="icon-magnifier icon" @click="query(1)"></el-button>
								</template>
							</el-input>
						</li>
						<li class="m-r-md m-t-xs">
							<button class="btn btn-success" @click="model={}; model.isEnabled='1'; dialogFormVisible = true"><i class="fa fa-plus-circle"></i>
								新增</button>
						</li>
					</ul>
				</header>
				<div class="panel-body">
					<el-table size="medium" :data="roleList" stripe style="width: 100%" border>
						<el-table-column fixed type="index" width="auto" :index="indexMethod">
						</el-table-column>
						<el-table-column prop="roleName" label="角色名称" width="200">
						</el-table-column>
						<el-table-column prop="roleCode" label="角色编码" width="150">
						</el-table-column>
						<el-table-column prop="remark" label="备注">
						</el-table-column>
						<el-table-column align="center" label="是否可用" width="100">
							<template v-slot="scope">
								<el-switch v-model="scope.row.isEnabled" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
								 inactive-value="0">
								</el-switch>
							</template>
						</el-table-column>
						<el-table-column align="center" label="操作" width="250">
							<template v-slot="scope">
								<el-button type="text" @click="edit(scope.row)">编辑</el-button>
								<el-divider direction="vertical"></el-divider>
								<el-button type="text" @click="deleteRole(scope.row.id)">删除</el-button>
								<el-divider direction="vertical"></el-divider>
								<el-button type="text" @click="openMenuPanel(scope.row)">菜单</el-button>
								<el-divider direction="vertical"></el-divider>
								<el-button type="text" @click="edit(scope.row)">接口</el-button>
							</template>
						</el-table-column>
					</el-table>

					<pagination :total="total" :pageNo='pageNo' :pageSize='pageSize' @onPageChange="query"></pagination>
				</div>

				<el-dialog :title="model.id==null ? '新增': '编辑'" v-model="dialogFormVisible">
					<form class="form-horizontal" action="#" method="get" onsubmit="return false;">
						<div class="form-group">
							<label class="col-sm-2 control-label">角色名称</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.roleName" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">角色编码</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.roleCode" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">数据范围</label>
							<div class="col-sm-10">
								<el-select v-model="model.dataScope" clearable>
									<el-option v-for="item in dataScopes" :key="item.code" :label="item.name" :value="item.code">
									</el-option>
								</el-select>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">是否可用</label>
							<div class="col-sm-10">
								<el-switch v-model="model.isEnabled" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
								 inactive-value="0">
								</el-switch>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">备注</label>
							<div class="col-sm-10">
								<input type="text" v-model.trim="model.remark" class="form-control parsley-validated" required="true">
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<div class="col-sm-10 col-sm-offset-2">
								<button type="button" class="btn btn-success" @click="submit()">提交</button>
							</div>
						</div>
					</form>
				</el-dialog>

				<el-drawer title="授权菜单" v-model="isShowMenuPanel" direction="rtl" destroy-on-close @open="initMenuData">
					<form class="form-horizontal" action="#" method="get" onsubmit="return false;">
						<div class="form-group">
							<label class="col-sm-2 control-label">角色</label>
							<div class="col-sm-10">
								<p class="form-control-static"> {{ model.roleName }} </p>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">菜单</label>
							<div class="col-sm-10">
								<el-tree ref="menuTree" :data="menuTreeData" default-expand-all show-checkbox node-key="id" :props="defaultTreeProps"
								 :check-strictly="true">
								</el-tree>
							</div>
						</div>
						<div class="line line-dashed b-b line-lg pull-in"></div>
						<div class="form-group">
							<div class="col-sm-12 text-right">
								<button type="button" class="btn btn-success" @click="setMenu()">提交</button>
								&nbsp;
								<button type="button" class="btn btn-success m-r-md" @click="resetMenu()">重置</button>
							</div>
						</div>
					</form>
				</el-drawer>

			</section>
		</section>
	</section>

</template>

<script>
	import {
		ElMessage
	} from 'element-plus'

	export default {
		components: {

		},
		data() {
			return {
				dialogFormVisible: false,
				roleList: [],
				total: 0, // 记录总条数
				pageSize: 10, // 每页显示条数
				pageNo: 1, // 当前的页数
				keywords: '',
				model: {},
				dataScopes: [{
					name: '全部数据权限',
					code: '0'
				}, {
					name: '本部门数据权限',
					code: '1'
				}, {
					name: '本部门及以下数据权限',
					code: '2'
				}, {
					name: '自定数据权限',
					code: '3'
				}],
				isShowMenuPanel: false,
				menuTreeData: [],
				defaultTreeProps: {
					label: 'name'
				}
			}
		},
		mounted() {
			this.query(1);
		},
		methods: {
			indexMethod(index) {
				return this.pageSize * (this.pageNo - 1) + index + 1;
			},
			submit() {
				if (this.model.id == null) {
					this.$api.role.add(this.model).then(data => {
						this.model.id = data.id;
						this.query(1);
					});
				} else {
					this.$api.role.update(this.model);
				}
				this.dialogFormVisible = false;
			},
			query(pageNo) {
				this.pageNo = null == pageNo ? 1 : pageNo;
				this.$api.role.query({
					roleName: this.keywords,
					pageSize: this.pageSize,
					pageNo: this.pageNo
				}).then(data => {
					this.roleList = data.rows;
					this.pageNo = data.pageNo;
					if (1 == this.pageNo) {
						this.total = data.total;
					}
				});
			},
			edit(model) {
				this.model = model;
				this.dialogFormVisible = true;
			},
			deleteRole(id) {
				this.$confirm('您确定要执行删除操作吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$api.role.delete(id).then(data => {
						this.query(this.pageNo);
					});
				});
			},
			//授权菜单
			openMenuPanel: async function(model) {
				this.model = model;
				console.debug("openMenuPanel", 1);
				//查询全部菜单
				await this.$api.menu.tree({
					// pageSize: this.pageSize,
					// pageNo: this.pageNo
				}).then(data => {
					this.menuTreeData = data;
					console.debug("openMenuPanel", 2);
				});

				this.isShowMenuPanel = true; //显示菜单授权Div
				console.debug("openMenuPanel", 3);
			},
			initMenuData(){
				this.resetMenu();
			},
			setMenu() {
				const selectedMenuIds = this.$refs.menuTree.getCheckedKeys();
				const halfMenuIds = this.$refs.menuTree.getHalfCheckedKeys(); //半含父节点
				this.$api.role.setMenuOfRole(this.model.id, selectedMenuIds.concat(halfMenuIds)).then(data => {
					this.isShowMenuPanel = false;
					ElMessage({
						message: '授权成功',
						type: 'success'
					});
				});
			},
			resetMenu() {
				//查询已授权菜单id
				this.$api.role.getMenuIdsOfRole(this.model.id).then(data => {
					console.debug("openMenuPanel", 4);
					this.$refs.menuTree.setCheckedKeys(data);
				});
			}
		}
	}
</script>

<style>
</style>
